package waal.pc.frontend.config;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import waal.pc.backend.channels.Channel;
import waal.pc.backend.module.Module;
import waal.pc.frontend.Surface;
import waal.pc.frontend.tiles.ChannelTile;
import waal.pc.frontend.tiles.TileHandler;

public class ChannelArea extends JSplitPane implements  ListSelectionListener, FocusListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4624239109423764203L;
	private WaalConfig wc;
	private TileHandler tileHandler;
	private DefaultListModel<Channel> channelListModel;
	private JList<Channel> channelList;
	private JPanel buttonPanel;
	private Map<Module, ChannelList> channels;
	private GridBagLayout bgbl;
	private JLabel jLheadline;
	private JLabel jLChannelNumber;
	private JButton activeButton;
	private JLabel jLChannelInput;
	private ChannelList activeListModel = null;
	private JButton addTileButton;
	private JComboBox<String> tileBox;
	private JComboBox<Integer> tileNumberBox;
	
	public ChannelArea(WaalConfig wc, TileHandler tileHandler) {
		super(JSplitPane.VERTICAL_SPLIT);
		this.wc = wc;
		this.tileHandler = tileHandler;
		this.channels = new HashMap<Module, ChannelList>();
		this.setOneTouchExpandable(true);
		this.setDividerLocation(250);
		this.setLeftComponent(this.makeTopPanel());
		this.setRightComponent(this.makeButtonPanel());
		
	
	}
	
	private JPanel makeTopPanel() {
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
		topPanel.add(new JLabel("Kanal Liste"));
		this.channelListModel = new DefaultListModel<Channel>();
		
		this.channelList = new JList<Channel>(channelListModel);
		this.channelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.channelList.addListSelectionListener(this);
		this.channelList.addFocusListener(this);
		this.channelList.setMaximumSize(new Dimension(200,1000));
		this.channelList.setMinimumSize(new Dimension(100,100));
		//this.moduleList.setCellRenderer(new HubListCellRenderer());
		topPanel.add(this.channelList);
		return topPanel;
	}
	
	public void makeChannelsForModule(Module module) {
		//Log.addToDebug(3, "ChannelArea", "makeChannelsForModule", "make Channels for module: " + module.getNumber() + "-" + module.getName() );
		if(!this.channels.containsKey(module)) {
			this.channels.put(module, new ChannelList(module, this));	
		}
	}
	
	public void setActiveModule(Module module) {
		if(this.activeListModel != null) {
			this.activeListModel.setLastIndex(this.channelList.getSelectedIndex());
			
		}
		this.activeListModel = this.channels.get(module);
		this.channelList.setModel(this.activeListModel);
		this.channelList.setSelectedIndex(this.activeListModel.getLastIndex());
		if(this.channelList.getSelectedValue() != null) {
			this.updateButton(this.channelList.getSelectedValue());
		}
	}
		
	private JPanel makeButtonPanel() {
		this.bgbl = new GridBagLayout();
		this.buttonPanel = new JPanel(this.bgbl);
		this.jLheadline = new JLabel();
		this.jLChannelNumber = new JLabel();
		this.jLChannelInput = new JLabel();
		
		this.tileNumberBox = new JComboBox<Integer>();
		for(int i = 1; i <= 6; i ++) {
			this.tileNumberBox.addItem(i);
		}
		
		this.tileBox = new JComboBox<String>();
		ActionListener alTileComboChange = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(tileBox.getSelectedItem() != null) 
				addTileButton.setText("Set Tile: " + tileBox.getSelectedItem().toString() + " to Position: " + tileNumberBox.getSelectedItem());
			}
		};
		this.tileNumberBox.addActionListener(alTileComboChange);
		this.tileBox.addActionListener(alTileComboChange);

		this.addTileButton = new JButton("Add");
		ActionListener alAddTile = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(tileBox.getSelectedItem() != null) {
					ChannelTile t = (ChannelTile) tileHandler.getTile(false, (String) tileBox.getSelectedItem());
					wc.addTileToSurface(t.getTile(channelList.getSelectedValue()), (int)tileNumberBox.getSelectedItem());
					
				}
			}
		};
		this.addTileButton.addActionListener(alAddTile);
		
		this.activeButton = new JButton("active");
		ActionListener alActive = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
			}
		};
		this.activeButton.addActionListener(alActive);
		this.activeButton.setEnabled(false);
		
		//															x, y, width, height, weightx, weighty )
		this.addButtonComponent(this.jLheadline, 					0, 0, 2, 1, 1.0, 0.0);
		this.addButtonComponent(new JLabel("Kanal Number:"),		0, 1, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLChannelNumber, 				1, 1, 1, 1, 1.0, 0.0);	
		this.addButtonComponent(new JLabel("Art:"),					0, 2, 1, 1, 0.0, 0.0);
		this.addButtonComponent(this.jLChannelInput, 				1, 2, 1, 1, 1.0, 0.0);
		this.addButtonComponent(this.activeButton,	 				0, 3, 2, 1, 1.0, 0.0);
		this.addButtonComponent(this.tileNumberBox,	 				0, 4, 1, 1, 1.0, 0.0);
		this.addButtonComponent(this.tileBox,	 					1, 4, 1, 1, 1.0, 0.0);
		this.addButtonComponent(this.addTileButton,	 				0, 5, 2, 1, 1.0, 0.0);
		return this.buttonPanel;
	}
	
	public void updateButton(Channel channel) {
		this.jLheadline.setText("Kanal: " + (channel.getChannelNumber()+1));
		this.jLChannelNumber.setText("" + (channel.getChannelNumber()+1));
		
		this.tileBox.setModel(this.tileHandler.getTileList(channel.isDigital(), false, channel.isInput(), !channel.isInput()));
		
		if(channel.isInput()) {
			this.jLChannelInput.setText("Eingang");
		} else {
			this.jLChannelInput.setText("Ausgang");
		}
		
		if(channel.isActive()) {
			this.activeButton.setText("active");
		} else {
			this.activeButton.setText("inactive");
		}
		
	}
	
	
	private void addButtonComponent(Component c, int x, int y, int width, int height, double weightx, double weighty ) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = x; gbc.gridy = y;
		gbc.gridwidth = width; gbc.gridheight = height;
		gbc.weightx = weightx; gbc.weighty = weighty;
		gbc.ipadx = 10; gbc.ipady = 5;
		this.bgbl.setConstraints( c, gbc );
		c.setPreferredSize(new Dimension(Surface.tile_x, Surface.tile_y));
		this.buttonPanel.add( c );
		//this.surface.pack();
	}
	
	@Override
	public void focusGained(FocusEvent arg0) {
		if(this.channelList.getSelectedValue() != null) {
			this.updateButton(this.channelList.getSelectedValue());
		}
		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if(!e.getValueIsAdjusting()) {
			return;
		} else {
			Channel channel = this.channelList.getSelectedValue();
			this.updateButton(channel);
			//this.wc.setChannelArea(this.channels.get(module));
		}	
	}
	
	public void updateFreeTileNumber(int tile) {
		this.tileNumberBox.setSelectedIndex(tile-1);
	}
}
